package com.personalink.management.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * 冷却服务
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CooldownService {
    
    private final RedisTemplate<String, Object> redisTemplate;
    
    private static final String COOLDOWN_KEY_PREFIX = "cooldown:persona:";
    
    /**
     * 检查用户是否可以切换人格
     */
    public boolean canSwitch(String userId) {
        String key = COOLDOWN_KEY_PREFIX + userId;
        return !redisTemplate.hasKey(key);
    }
    
    /**
     * 设置冷却时间
     */
    public void setCooldown(String userId, int seconds) {
        String key = COOLDOWN_KEY_PREFIX + userId;
        redisTemplate.opsForValue().set(key, System.currentTimeMillis(), seconds, TimeUnit.SECONDS);
        log.debug("为用户 {} 设置人格切换冷却时间: {} 秒", userId, seconds);
    }
    
    /**
     * 获取剩余冷却时间（秒）
     */
    public long getRemainingCooldown(String userId) {
        String key = COOLDOWN_KEY_PREFIX + userId;
        Long ttl = redisTemplate.getExpire(key, TimeUnit.SECONDS);
        return ttl != null && ttl > 0 ? ttl : 0;
    }
    
    /**
     * 清除冷却时间
     */
    public void clearCooldown(String userId) {
        String key = COOLDOWN_KEY_PREFIX + userId;
        redisTemplate.delete(key);
        log.debug("清除用户 {} 的人格切换冷却时间", userId);
    }
    
    /**
     * 检查并获取冷却状态
     */
    public CooldownStatus getCooldownStatus(String userId) {
        boolean canSwitch = canSwitch(userId);
        long remainingTime = canSwitch ? 0 : getRemainingCooldown(userId);
        
        return CooldownStatus.builder()
                .canSwitch(canSwitch)
                .remainingSeconds(remainingTime)
                .build();
    }
    
    /**
     * 冷却状态信息
     */
    public static class CooldownStatus {
        private final boolean canSwitch;
        private final long remainingSeconds;
        
        private CooldownStatus(boolean canSwitch, long remainingSeconds) {
            this.canSwitch = canSwitch;
            this.remainingSeconds = remainingSeconds;
        }
        
        public static CooldownStatusBuilder builder() {
            return new CooldownStatusBuilder();
        }
        
        public boolean isCanSwitch() {
            return canSwitch;
        }
        
        public long getRemainingSeconds() {
            return remainingSeconds;
        }
        
        public static class CooldownStatusBuilder {
            private boolean canSwitch;
            private long remainingSeconds;
            
            public CooldownStatusBuilder canSwitch(boolean canSwitch) {
                this.canSwitch = canSwitch;
                return this;
            }
            
            public CooldownStatusBuilder remainingSeconds(long remainingSeconds) {
                this.remainingSeconds = remainingSeconds;
                return this;
            }
            
            public CooldownStatus build() {
                return new CooldownStatus(canSwitch, remainingSeconds);
            }
        }
    }
}